package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String, String>> uploadTrack(MultipartFile file) {
		Map<String, String> mapResult = vodService.uploadTrack(file);
		return Result.ok(mapResult);
	}

	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		//1.获取用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.调用业务层新增声音
		trackInfoService.saveTrackInfo(userId, trackInfoVo);
		return Result.ok();
	}

	@GuiGuLogin
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable("page") Integer page,
													   @PathVariable("limit") Integer limit,
													   @RequestBody TrackInfoQuery trackInfoQuery){
		Long userId = AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		Page<TrackListVo> pageInfo = new Page<>(page, limit);
		pageInfo = trackInfoService.findUserTrackPage(pageInfo,trackInfoQuery);
		return Result.ok(pageInfo);
	}

	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable("id") Long id){
		TrackInfo trackInfo = trackInfoService.getTrackInfo(id);
		return Result.ok(trackInfo);
	}

	@GuiGuLogin
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable("id") Long id,
								   @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}

	@GuiGuLogin
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	///api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
	@GuiGuLogin(required = false)
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(
			@PathVariable("albumId") Long albumId,
			@PathVariable("page") Integer page,
			@PathVariable("limit") Integer limit
	){
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
		Page<AlbumTrackListVo> result = trackInfoService.getAlbumTrackPage(pageInfo,albumId,userId);
		return Result.ok(result);
	}

	/**
	 * 根据声音ID，获取声音统计信息
	 * @param trackId
	 * @return
	 */
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		return Result.ok(trackInfoService.getTrackStatVo(trackId));
	}

	///api/album/trackInfo/findUserTrackPaidList/{trackId}
	@GuiGuLogin
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String, Object>>> getUserWaitBuyTrackPayList(@PathVariable Long trackId){
		Long userId = AuthContextHolder.getUserId();
		List<Map<String, Object>> map = trackInfoService.getUserWaitBuyTrackPayList(trackId);
		return Result.ok(map);
	}

	@GuiGuLogin
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> getWaitBuyTrackInfoList(@PathVariable Long trackId, @PathVariable int trackCount) {
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> trackInfoList = trackInfoService.getWaitBuyTrackInfoList(userId, trackId, trackCount);
		return Result.ok(trackInfoList);
	}
}

